<?php
/**
 * Created by PhpStorm.
 * User: caipeichao
 * Date: 4/4/14
 * Time: 9:29 AM
 */

namespace App\Controller;

use App\Model;
use Think\Controller;


class WeiboController extends BaseController
{


    /**获取微博字数限制
     * @auth 陈一枭
     */
    public function getWeiboWordsLimit()
    {
        C(api('Config/lists'));
        $limit=modC('WEIBO_NUM',140,'WEIBO');
        $list=array('limit'=>$limit);
        $this->apiSuccess('微博字数限制获取成功。',$list);
    }

    public function listAllWeibo()
    {

        //获取参数
        $uid = I('uid', 0, 'intval');
        $aPage = I('page', 1, 'intval');
        $aCount = I('count', 10, 'intval');
        $map = array();
        if ($uid != 0) {
            $map['uid'] = $uid;
        }
        $loadCount = I('loadcount', 1, 'intval');;
        $lastId = I('lastId', 0, 'intval');;
        //获取微博列表
        if (isset($keywords) && $keywords != '') {
            $map['content'] = array('like', "%{$keywords}%");
        }
        $map['status'] = 1;


        //获取微博列表


        if ($aPage == 1 && $loadCount == 1) {

            $weibo = D('Weibo')->field('id')->where($map)->order('is_top desc,create_time desc')->limit(10)->select();

        } elseif ($aPage == 1 && $loadCount > 1 && $loadCount <= 3) {

            $is_top = D('weibo')->field('id,is_top')->where(array('id' => $lastId))->getField('is_top');
            if (!$is_top) {
                $map['id'] = array('lt', $lastId);
                $weibo = D('Weibo')->field('id')->where($map)->order('create_time desc')->limit(10)->select();
            } else {
                $ids = D('Weibo')->field('id,is_top')->where(array('id' => array('egt', $lastId), 'is_top' => 1))->field('id')->select();
                $ids = getSubByKey($ids, 'id');
                $ids = implode(",", $ids);
                $map['_string'] = '(id < ' . $lastId . ' AND is_top =1 ) OR (id > 0  AND (id NOT IN (' . $ids . '))) ';
                $weibo = D('Weibo')->field('id,is_top')->where($map)->order('is_top desc,create_time desc')->limit(10)->select();
            }


        } elseif ($aPage > 1) {
            $weibo = D('Weibo')->field('id')->where($map)->order('is_top desc,create_time desc')->page($aPage, $aCount)->select();


        }


        //获取每个微博详情
        foreach ($weibo as &$e) {


            $e = $this->getWeiboStructure($e['id']);

        }
        unset($e);
        $list = array('list' => $weibo);
        //返回微博列表


        $this->apiSuccess('获取成功', $list);
    }


    public function getHotWeibo()
    {
        $aPage = I('page', 1, 'op_t');
        $aCount = I('count', 10, 'op_t');

        $hot_left = modC('HOT_LEFT', 3);
        $time_left = get_some_day($hot_left);
        $param['create_time'] = array('gt', $time_left);
        $param['status'] = 1;
        $param['is_top'] = 0;
        $weibo = D('Weibo')->where(array('status' => 1, $param))->page($aPage, $aCount)->order('comment_count desc')->select();
        $totalCount = D('Weibo')->where(array('status' => 1, $param))->count();
        if ($totalCount <= $aPage * $aCount) {
            $pid['count'] = 0;
        } else {
            $pid['count'] = 1;
        }

        //获取每个微博详情
        foreach ($weibo as &$e) {


            $e = $this->getWeiboStructure($e['id']);

        }
        unset($e);
        $list = array('list' => $weibo);
        //返回微博列表


        $this->apiSuccess('获取成功', $list);
    }

    //列出我关注的全部微博，需要登录

    public function listMyFollowingWeibo()
    {
        $this->requireLogin();

        $aPage = I('page', 1, 'op_t');
        $aCount = I('count', 10, 'op_t');
        $follow_who_ids = D('Follow')->where(array('who_follow' => is_login()))->field('follow_who')->select();
        if($follow_who_ids){
            $follow_who_ids = array_column($follow_who_ids, 'follow_who');//简化数组操作。
            $follow_who_ids = array_merge($follow_who_ids, array(is_login()));//加上自己的微博
            $map['uid'] = array('in', $follow_who_ids);
        }else{
            $map['uid']=is_login();
        }

        $weibo = D('Weibo')->where(array('status' => 1, $map))->page($aPage, $aCount)->order('create_time desc')->field('id')->select();//我关注的人的微博


        $totalCount = D('Weibo')->where(array('status' => 1, $map))->count();
        if ($totalCount <= $aPage * $aCount) {
            $pid['count'] = 0;
        } else {
            $pid['count'] = 1;
        }

        foreach ($weibo as &$v) {
            $v = $this->getWeiboStructure($v['id']);

        }
        $list = array('list' => $weibo);
        //返回微博列表


        $this->apiSuccess('获取成功', $list);
    }


    //发微博，需要登录

    public function sendWeibo()
    {
        $this->requireLogin();

        $aContent = I('content', '', 'op_t');
        $aType = I('type', 'image', 'op_t');
        $aAttachIds = I('attach_ids', '', 'op_t');
        $aFrom = I('from', '', 'op_t');

        //权限判断
        if (!is_login()) {
            $this->apiError('请登陆后再进行操作');
        }


        if (!check_auth('Weibo/Index/doSend')) {
            $this->apiError('您无微博发布权限。');
        }

        if (empty($aContent)) {
            $this->apiError('发布内容不能为空。');
        }

        $return = check_action_limit('add_weibo', 'weibo', 0, is_login(), true);
        if ($return && !$return['state']) {
            $this->apiError($return['info']);
        }
        $feed_data = array();
        $feed_data['attach_ids'] = $aAttachIds;
        if (empty($aAttachIds)) {
            $aType = 'feed';
        }

        // 执行发布，写入数据库
        if ($aFrom) {
            $data = array('from' => $aFrom, 'uid' => is_login(), 'create_time' => time(), 'type' => $aType, 'status' => 1, 'content' => $aContent, 'data' => serialize($feed_data),);
        } else {
            $data = array('uid' => is_login(), 'create_time' => time(), 'type' => $aType, 'status' => 1, 'content' => $aContent, 'data' => serialize($feed_data), 'from' => '安卓客户端');
        }

        D('Weibo/Topic')->add($aContent);
        $weibo_id = D('Weibo')->add($data);
        if (!$weibo_id) {
            return false;
        }
        action_log('add_weibo', 'weibo', $weibo_id, is_login());
        $uids = get_at_uids($aContent);

        $my_username = query_user('nickname');

        foreach ($uids as $uid) {
            $message = '内容：' . $aContent;
            $title = $my_username . '@了您';
            $fromUid = get_uid();
            $messageType = 1;
            D('Common/Message')->sendMessage($uid, $title, $message, 'Weibo/Index/weiboDetail', array('id' => $weibo_id), $fromUid, $messageType);
        }

        clean_query_user_cache(is_login(), array('weibocount'));

        $weibo = D('Weibo')->where(array('id' => $weibo_id, 'status' => 1))->find();
        $weibo['id'] = intval($weibo['id']);
        $weibo['support_count'] = D('Support')->where(array('appname' => 'Weibo', 'table' => 'weibo', 'row' => $weibo['id']))->count();
        $weibo['content'] = text($weibo['content']);
        $weibo['create_time'] = intval($weibo['create_time']);
        $weibo['user'] = $this->getUserStructure($weibo['uid']);
        $weibo ['data'] = unserialize($weibo['data']);
        $weibo['cover_url'] = explode(',', $weibo['data']['attach_ids']);        //把attach_ids里的图片ID转出来
        foreach ($weibo['cover_url'] as &$a) {
            $weibo['images'][] = getThumbImageById($a, 100, 100);
        }
        $weibo['can_delete'] = $this->canDeleteWeibo($weibo);
        $detail = array('detail' => $weibo);


        if ($weibo) {
            $this->apiSuccess('发送成功', $detail);
        } else {
            $return['status'] = ' 0';
            $this->apiError('发送失败');
        }

    }

    /**删除微博
     * @param $weibo_id
     * @auth 陈一枭
     */
    public function deleteWeibo()
    {

        $this->requireLogin();

        $aWeiboId = I('weibo_id', 0, 'intval');


        //从数据库中删除微博、以及附属评论
        $result = D('weibo/weibo')->where(array('id' => $aWeiboId, 'status' => 1))->find();

        if (!$result) {
            $this->apiError('找不到此微博');
        }

        if (!is_administrator(is_login())) {

            //不是管理员则进行检测
            if ($result['uid'] != is_login()) {
                $this->apiError('无权管理');
            }
        }
        D('weibo/weibo')->where(array('id' => $aWeiboId))->save(array('status' => -1, 'comment_count' => 0));
        $result = D('weibo/weibo')->where(array('id' => $aWeiboId, 'status' => -1))->find();

        D('Weibo/WeiboComment')->where(array('weibo_id' => $aWeiboId))->setField('status', -1);
        action_log('del_weibo', 'weibo', $aWeiboId, is_login());
        if (!$result) {

            $this->apiError('删除失败');
        } else {


            $this->apiSuccess('删除成功');
        }

    }

    /*转发微博，需要登录*/
    public function sendRepost()
    {


        $this->requireLogin();
        $aContent = I('content', '', 'op_t');              //说点什么的内容
        $aSoueceId = I('sourceId', 0, 'intval');           //获取该微博源ID
        $aWeiboId = I('weiboId', 0, 'intval');             //要转发的微博的ID
        $aBeComment = I('release', 'false', 'op_t');
        $aType = I('type', '', 'op_t'); //要转发的微博的type
        if (empty($aContent)) {
            $this->apiError('转发内容不能为空');
        }

        $this->checkAuth('Weibo/Index/doSendRepost', -1, '您无微博转发权限。');

        $return = check_action_limit('add_weibo', 'weibo', 0, is_login(), true);
        if ($return && !$return['state']) {
            $this->apiError($return['info']);
        }

        $feed_data = '';

        $sourse = $this->getWeiboDetail($aSoueceId);

        $sourceweibo = $sourse['weibo'];

        $feed_data['source'] = $sourceweibo;
        $feed_data['sourceId'] = $aSoueceId;
        //发送微博
        $weibo = $this->send_weibo($aContent, $aType, $feed_data, $from = '手机安卓版');

        if ($aBeComment == 'true') {
           $this->send_comment($aWeiboId, $aContent);
        }
        if ($weibo) {
            D('weibo')->where('id=' . $aSoueceId)->setInc('repost_count');
            $aWeiboId != $aSoueceId && D('weibo/weibo')->where('id=' . $aWeiboId)->setInc('repost_count');
        }

        $user = query_user(array('nickname'), is_login());
        $toUid = D('weibo')->where(array('id' => $aWeiboId))->getField('uid');

        D('Common/Message')->sendMessage($toUid, $user['nickname'] . '转发了您的微博！', '转发提醒', 'Weibo/Index/weiboDetail', array('id' => $weibo['weibo_id']), is_login(), 1);
        $weibo=  $this->getWeiboStructure($weibo);

        $list = array('detail' => $weibo);
        //返回成功结果
        $this->apiSuccess('转发成功', $list);
    }

    function send_weibo($content,$type,$feed_data = '', $from = ''){

        $uid = is_login();


        $weibo_id = $this->addWeibo($uid, $content, $type, $feed_data,$from);
        if (!$weibo_id) {
            return false;
        }
        action_log('add_weibo', 'weibo', $weibo_id, $uid);
        $uids = get_at_uids($content);
        send_at_message($uids, $weibo_id, $content);
        clean_query_user_cache(is_login(), array('weibocount'));
        return $weibo_id;

    }

    public function addWeibo($uid, $content = '', $type = 'feed', $feed_data = array(), $from = '')
    {
        //写入数据库
        $data = array('uid' => $uid, 'content' => $content, 'type' => $type, 'data' => serialize($feed_data), 'from' => $from);
        $data = D('weibo/weibo')->create($data);
        if (!$data) return false;
        $weibo_id = D('weibo/weibo')->add($data);

        //返回微博编号
        return $weibo_id;
    }
    /**微博点赞，需要登录
     * @param $weibo_id
     * @auth 陈一枭
     */
    public function supportWeibo()
    {
        $this->requireLogin();
        $aWeiboId = I('weibo_id', 0, 'intval');
        $supportModel = D('Support');
        $weibo = array('appname' => 'Weibo', 'table' => 'weibo', 'row' => $aWeiboId);
        $weibo['uid'] = get_uid();
        if ($supportModel->where($weibo)->count()) {
            $this->apiError('点赞失败：' . '重复点赞。', 501);
        } else {
            $weibo['create_time'] = time();
            $result = $supportModel->add($weibo);
            if ($result) {
                $this->apiSuccess('点赞成功。');
            } else {
                $this->apiError('点赞失败：' . '数据存储错误。', 502);
            }
        }
    }

    /**发送评论，需要登录
     * @param     $weibo_id
     * @param     $content
     * @param int $comment_id
     * @auth 陈一枭
     */
    public function sendComment()
    {
        $this->requireLogin();


        $aContent = I('content', '', 'op_t');              //说点什么的内容
        $aWeiboId = I('weibo_id', 0, 'intval');             //要评论的微博的ID
        $aCommentId = I('to_comment_id', 0, 'intval');

        if (empty($aContent)) {
            $this->apiError('评论内容不能为空。');
        }

        $this->checkAuth('Weibo/Index/doComment', -1, '您无微博评论权限。');
        $return = check_action_limit('add_weibo_comment', 'weibo_comment', 0, is_login(), true);//行为限制
        if ($return && !$return['state']) {
            $this->apiError($return['info']);
        }
        $new_id = $this->send_comment($aWeiboId, $aContent, $aCommentId); //发布评论

        if ($new_id) {
            D('Weibo/Weibo')->where(array('status' => 1, 'id' => $aWeiboId))->setInc('comment_count');
        }
        $weibocomment = D('Weibo/WeiboComment')->where(array('status' => 1, 'id' => $new_id))->order('create_time desc')->select();

        foreach ($weibocomment as &$k) {
            $k['user'] = query_user(array('uid', 'nickname', 'avatar32', 'avatar64', 'avatar128', 'avatar256', 'avatar512', 'space_url', 'icons_html', 'rank_link', 'signature', 'tox_money', 'title', 'weibocount', 'fans', 'following'), $k['uid']);

            $k['content'] = parse_weibo_content($k['content']);
        }
        $this->apiSuccess('返回成功。', $weibocomment);
    }

    /**列出全部评论
     * @param     $weibo_id
     * @param int $page
     * @param int $count
     * @auth 陈一枭
     */
    public function listComment()
    {
        $aWeiboId = I('weibo_id', 0, 'intval');
        $aPage = I('page', 1, 'op_t');
        $aCount = I('count', 10, 'op_t');
        $result = D('Weibo/WeiboComment')->where(array('status' => 1, 'weibo_id' => $aWeiboId))->page($aPage, $aCount)->select();

        $fields = array('uid', 'nickname', 'avatar32', 'avatar64', 'avatar128', 'avatar256', 'avatar512', 'space_url', 'icons_html', 'rank_link', 'signature', 'tox_money', 'title', 'weibocount', 'fans', 'following');

        foreach ($result as &$c) {

            $c['user'] = query_user($fields, $c['uid']);
        }
        unset($c);

        $list = array('list' => $result);
        $this->apiSuccess('返回成功。', $list);
    }


    /**删除评论
     * @param $comment_id
     * @auth 陈一枭
     */
    public function deleteComment($comment_id)
    {
        $this->requireLogin();
        $comment= D('Weibo/WeiboComment')->where(array('status' => 1, 'id' => $comment_id))->find();
        $this->checkAuth(null, $comment['uid'], '您无删除微博评论权限。');
        $result = D('Weibo/WeiboComment')->where(array('status' => 1, 'id' => $comment_id))->delete();
        action_log('del_weibo_comment', 'weibo_comment', $comment_id, is_login());
        if ($result) {
            $return['status'] = 1;
            $return['info'] = '删除成功';
        } else {
            $return['status'] = 0;
            $return['info'] = '删除失败';
        }
        $this->apiSuccess($result);
    }


    /*——————————————————————私有函数————————————————————————————*/


    private function math_images($weibo)
    {
        if ($weibo['type'] == 'image') { //如果是图片微博则解析图片
            $attach_ids = explode(',', $weibo['data']['attach_ids']);
            foreach ($attach_ids as $data_id) {
                $weibo['images'][] = getThumbImageById($data_id, 100, 100);
            }
        } else if (($weibo['type'] == 'repost')) { //处理转发中的图片微博
            if ($weibo['data']['sourse']['type'] == 'image') {
                $source = $weibo['data']['sourse'];
                $attach_ids = explode(',', $source['data']['attach_ids']);
                foreach ($attach_ids as $data_id) {
                    $source['images'][] = getThumbImageById($data_id, 100, 100);
                }
                $weibo['data']['sourse'] = $source;
            }
        }
        return $weibo;
    }

    private function filterWeibo($aType, $param)
    {
        if ($aType == 'concerned') {
            $followList = D('Follow')->getFollowList();
            $param['where']['uid'] = array('in', $followList);
        }
        if ($aType == 'hot') {
            $hot_left = modC('HOT_LEFT', 3);
            $time_left = get_some_day($hot_left);
            $param['where']['create_time'] = array('gt', $time_left);
            $param['order'] = 'comment_count desc';
        }
        return $param;
    }

    /**预处理微博列表
     * @param $list
     * @return mixed
     * @auth 陈一枭
     */
    private function handleWeiboList($list)
    {
        foreach ($list as &$weibo) {
            $weibo = $this->preHandleWeibo($weibo);

        }
        return $list;
    }


    /**
     * @param $weibo
     * @return mixed
     * @auth 陈一枭
     */
    private function math_support($weibo)
    {
        $support_model = D('Support');
        $weibo_map = array('appname' => 'Weibo', 'table' => 'weibo', 'row' => $weibo['id']);
        $weibo['support_count'] = $support_model->where($weibo_map)->count();
        if (is_login()) {
            $weibo_supported_map = $weibo_map;
            $weibo_supported_map['uid'] = is_login();
            $weibo['is_supported'] = $support_model->where($weibo_supported_map)->count();
            return $weibo;
        }
        return $weibo;
    }


    /**
     * @param $weibo
     * @auth 陈一枭
     */
    private function clean_useless_data($weibo)
    {
        unset($weibo['fetchContent']);
        unset($weibo['user']['space_url']);
        unset($weibo['user']['avatar32']);
        unset($weibo['user']['avatar64']);
        unset($weibo['user']['avatar256']);
        unset($weibo['user']['avatar512']);
        unset($weibo['user']['icons_html']);
        return $weibo;
    }

    /**
     * @param $weibo
     * @return mixed
     * @auth 陈一枭
     */
    private function preHandleWeibo($weibo)
    {
        $weibo = $this->math_images($weibo);
        $weibo = $this->math_support($weibo);
        $weibo = $this->clean_useless_data($weibo);
        return $weibo;
    }


    /**
     * send_comment  发布评论
     * @param $weibo_id
     * @param $content
     * @param int $comment_id
     * @return bool
     * @author:xjw129xjt(肖骏涛) xjt@ourstu.com
     */
    private function send_comment($weibo_id, $content, $comment_id = 0)
    {

        $uid = is_login();
        $result = D('WeiboComment')->add(array('uid' => $uid, 'status' => 1, 'content' => $content, 'weibo_id' => $weibo_id, 'to_comment_id' => $comment_id, 'create_time' => time()));
        if (!$result) {
            return false;
        }
        //行为日志
        action_log('add_weibo_comment', 'weibo_comment', $result, $uid);
        //通知微博作者
        $weibo = $this->getWeiboDetail($weibo_id);

        $this->send_comment_message($weibo['uid'], $weibo_id, "评论内容：$content");
        //通知回复的人
        if ($comment_id) {
            $comment = D('WeiboComment')->getComment($comment_id);
            if ($comment['uid'] != $weibo['uid']) {
                $this->send_comment_message($comment['uid'], $weibo_id, "回复内容：$content");
            }
        }

        $uids = get_at_uids($content);
        $uids = array_subtract($uids, array($weibo['uid'], $comment['uid']));
        $this->send_at_message($uids, $weibo_id, $content);
        return $result;
    }
    public function getComment($id){
        $comment = S('weibo_comment_'.$id);
        if(!$comment){
            $comment = D('Weibo/Weibo')->find($id);
            $comment['content']=parse_comment_content($comment['content']);
            $comment['user'] = query_user( array('uid', 'nickname', 'avatar32', 'avatar64', 'avatar128', 'avatar256', 'avatar512', 'space_url', 'icons_html', 'rank_link', 'score', 'title', 'weibocount', 'fans', 'following'), $comment['uid']);
            S('weibo_comment_'.$id,$comment);
        }
        $comment['can_delete']  = is_administrator(is_login()) || $comment['uid'] == is_login();
        return $comment;
    }
    /**
     * send_comment_message 发送评论消息
     * @param $uid
     * @param $weibo_id
     * @param $message
     * @author:xjw129xjt(肖骏涛) xjt@ourstu.com
     */
    private function send_comment_message($uid, $weibo_id, $message)
    {
        $title = '评论消息';
        $from_uid = is_login();
        $type = 1;
        D('Common/Message')->sendMessage($uid, $title, $message, 'Weibo/Index/weiboDetail', array('id' => $weibo_id), $from_uid, $type);
    }


    /**
     * send_at_message  发送@消息
     * @param $uids
     * @param $weibo_id
     * @param $content
     * @author:xjw129xjt(肖骏涛) xjt@ourstu.com
     */
    private function send_at_message($uids, $weibo_id, $content)
    {
        $my_username = query_user('nickname');
        foreach ($uids as $uid) {
            $message = '内容：' . $content;
            $title = $my_username . '@了您';
            $fromUid = get_uid();
            $messageType = 1;
            D('Common/Message')->sendMessage($uid, $title, $message, 'Weibo/Index/weiboDetail', array('id' => $weibo_id), $fromUid, $messageType);
        }
    }

    private function parse_topic($content)
    {
        //找出话题
        $topic = get_topic($content);

        //将##替换成链接
        foreach ($topic as $e) {
            $tik = D('Weibo/Topic')->where(array('name' => $e))->find();

            //没有这个话题的时候创建这个话题
            if ($tik) {
                //D('Weibo/Topic')->add(array('name'=> $e));
                $space_url = U('Weibo/Topic/index', array('topk' => urlencode($e)));
                if (modC('HIGH_LIGHT_TOPIC', 1, 'Weibo')) {
                    $content = str_replace("#$e#", " <a class='label label-badge label-info'  href=\"$space_url\" target=\"_blank\">#$e# </a> ", $content);
                } else {
                    $content = str_replace("#$e#", " <a href=\"$space_url\" target=\"_blank\">#$e# </a> ", $content);
                }
            }
        }

        //返回替换的文本
        return $content;
    }

    private function get_topic($content)
    {
        //正则表达式匹配
        $topic_pattern = "/#([^\\#|.]+)#/";
        preg_match_all($topic_pattern, $content, $users);

        //返回话题列表
        return array_unique($users[1]);
    }


    function use_topic()
    {
        $topic = modC('USE_TOPIC');

        if (empty($topic)) {
            return;
        }
        $topics = explode(',', $topic);
        $html = '';
        foreach ($topics as $k => $v) {
            $v = '#' . $v . '#';
            $html .= '<a href="javascript:" class="label label-badge label-info" data-role="chose_topic">' . $v . '</a> ';
        }
        unset($k, $v);

        return $html;

    }

    private function getWeiboStructure($id)
    {
        $fields = array('uid', 'nickname', 'avatar32', 'avatar64', 'avatar128', 'avatar256', 'avatar512', 'space_url', 'icons_html', 'rank_link', 'signature', 'tox_money', 'title', 'weibocount', 'fans', 'following');
        $weibo = D('Weibo/Weibo')->find($id);

        $weibo_data = unserialize($weibo['data']);

        $weibo_sorse = D('Weibo/Weibo')->where(array('id' => $weibo_data['sourceId'], 'status' => 1))->find();

        $weibo_sorse['user'] = query_user($fields, $weibo_sorse['uid']);
        $weibo_sorse['data'] = unserialize($weibo_sorse['data']);

        $weibo_sorse['cover_url'] = explode(',', $weibo_sorse['data']['attach_ids']);        //把attach_ids里的图片ID转出来

        foreach ($weibo_sorse['cover_url'] as &$a) {
            $weibo_sorse['images'][] = getThumbImageById($a, 100, 100);

        }

        $weibo_sorse['support_count'] = D('Support')->where(array('appname' => 'Weibo', 'table' => 'weibo', 'row' => $weibo_data['sourceId']))->count();
        $weibo_sorse ['weibo_data'] = $weibo_sorse ['data'];


        $weibo['id'] = intval($weibo['id']);
        $weibo['support_count'] = D('Support')->where(array('appname' => 'Weibo', 'table' => 'weibo', 'row' => $weibo['id']))->count();
        $weibo['content'] = text($weibo['content']);
        $weibo['create_time'] = intval($weibo['create_time']);

        $weibo ['data'] = unserialize($weibo['data']);
        $weibo['cover_url'] = explode(',', $weibo['data']['attach_ids']);        //把attach_ids里的图片ID转出来

        foreach ($weibo['cover_url'] as &$a) {
            $weibo['images'][] = getThumbImageById($a, 100, 100);

        }
        $weibo ['data']['sourse'] = $weibo_sorse;
        $support['appname'] = 'Weibo';                              //查找是否点赞
        $support['table'] = 'weibo';
        $support['uid'] = is_login();
        $is_support = D('Support')->where($support)->select();

        $is_support = array_column($is_support, 'row');
        if (in_array($weibo['id'], $is_support)) {                         //判断是否已经点赞
            $weibo['is_supported'] = '1';
        } else {
            $weibo['is_supported'] = '0';
        }

        $weibo ['weibo_data'] = $weibo ['data'];
        $weibo ['comment_count'] = intval($weibo['comment_count']);
        $weibo['repost_count'] = intval($weibo['repost_count']);
        $weibo ['can_delete'] = 0;
        $weibo['user'] = query_user($fields, $weibo['uid']);


        $weibo['can_delete'] = $this->canDeleteWeibo($weibo);
        return $weibo;

    }

    protected function getUserStructure($uid)
    {
        //请不要在这里增加用户敏感信息，可能会暴露用户隐私
        $fields = array('uid', 'nickname', 'avatar32', 'avatar64', 'avatar128', 'avatar256', 'avatar512', 'space_url', 'icons_html', 'rank_link', 'signature', 'score', 'tox_money', 'title', 'weibocount', 'fans', 'following');
        return query_user($fields, $uid);
    }

    private function getWeiboDetail($id)
    {


        $weibo = D('Weibo/Weibo')->where(array('status' => 1, 'id' => $id))->find();
        if (!$weibo) {
            return null;
        }
        $weibo_data = unserialize($weibo['data']);

        $weibo = array(
            'id' => intval($weibo['id']),
            'content' => text($weibo['content']),
            'create_time' => intval($weibo['create_time']),
            'type' => $weibo['type'],
            'data' => unserialize($weibo['data']),
            'weibo_data' => $weibo_data,
            'comment_count' => intval($weibo['comment_count']),
            'repost_count' => intval($weibo['repost_count']),
            'can_delete' => 0,
            'is_top' => $weibo['is_top'],
            'uid' => $weibo['uid'],
            'from' => $weibo['from']

        );


        $weibo['user'] = query_user(array('uid', 'nickname', 'avatar64', 'space_url', 'rank_link', 'title'), $weibo['uid']);


        return $weibo;
    }

    private function canDeleteWeibo($weibo)
    {
        //如果是管理员，则可以删除微博
        if (is_administrator(get_uid()) || check_auth('deleteWeibo')) {
            return true;
        }

        //如果是自己发送的微博，可以删除微博
        if ($weibo['uid'] == get_uid()) {
            return true;
        }

        //返回，不能删除微博
        return false;
    }
}